package com.controller;

import com.pojo.*;
import com.service.*;
import com.vo.ParkingCarVo;
import com.vo.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Controller
@RequestMapping("/MySelfCarLot")
public class MySelfCarLotController {
@Resource
private ParkingCarService parkingCarService;
@Resource
private StartStopSelfService startStopSelfService;
@Resource
private ParkingLotService parkingLotService;
@Resource
private OwnersService ownerService;
@Resource
private ParkService parkService;
@Resource
private TrafficrecordsService trafficreCordsService;
@Resource
private VehicleService vehicleService;
@RequestMapping("/toMySelfCarLot")
    public String toMySelfCarLot(){
    return "/CommunityParking/MySelfCarLot";
}
@ResponseBody
@RequestMapping("/selectParkingCarByOwnerId")
    public Result selectParkingCarByOwenrId(Integer ownerId){
    List<Parkingcar> parkingcars = parkingCarService.selectParkingCarByOwnerId(ownerId);
    return Result.success(parkingcars);
}
    @ResponseBody
    @RequestMapping("/selectStartStopByOwnerId")
    public Result selectStartStopByOwnerId(Integer ownerid){
        List<Startstopself> startstops = startStopSelfService.selectStartStopByOwnerId(ownerid);
        return Result.success(startstops);
    }
@ResponseBody
    @RequestMapping("addStartStop")
    public Result addStartStop(Startstopself startstopself){
    LocalDateTime localDateTime = LocalDateTime.now();
    String starttime = localDateTime.toString();
    startstopself.setStarttime(starttime);
    Parkinglot parkinglot = parkingLotService.selectParkingLotById(startstopself.getParkinglotid());
    startstopself.setParkingcode(parkinglot.getParkingcode());
    startstopself.setParkid(parkinglot.getParkid());
    int i = startStopSelfService.addStartStop(startstopself);
    return Result.success("开始计时");
}
    @ResponseBody
    @RequestMapping("/updateParklotStateById")
    public Result updateParkLotStateById(Integer id){
    try {
        int i = parkingCarService.updateParkinglotStateById(id);
        return Result.success("成功");
    }catch (Exception e){
        e.printStackTrace();
        return Result.success("失败");
    }

    }
    @ResponseBody
    @RequestMapping("/updateParklotStateByTwo")
    public Result updateParklotStateByTwo(ParkingCarVo parkingCarVo){
        System.out.println(parkingCarVo);
    try {
           int i = parkingCarService.updateParkingLotStateByTwo(parkingCarVo);
           return Result.success("成功");
       }catch (Exception e){
           e.printStackTrace();
           return Result.success("失败");
       }

    }
    @ResponseBody
    @RequestMapping("/selectStartStopByParkLotId")
    public Result selectStartStopByParkLotId(Integer parklotid) {
        Startstopself startstopself = startStopSelfService.selectStartStopByParkLotId(parklotid);
        return  Result.success(startstopself);
    }
    @RequestMapping("/deleteStartStopById")
    @ResponseBody
    public Result deleteStartStopById(Integer id){
        int i = startStopSelfService.deleteStartStopById(id);
        return Result.success("成功");
    }
    @ResponseBody
    @RequestMapping("/selectParkingLotById")
    public Result selectParkingLotById(Integer id){
        Parkinglot parkinglot = parkingLotService.selectParkingLotById(id);
        return Result.success(parkinglot);
    }
    @ResponseBody
    @RequestMapping("/selectParkingById")
    public Result selectParkingById(Integer id){
        Park park = parkService.selectParkById(id);
        return Result.success(park);
    }
    @ResponseBody
    @RequestMapping("/selectCarById")
    public Result selectVarById(Integer id){
        Vehicle vehicle = vehicleService.selectVehicleById(id);
        return Result.success(vehicle);
    }

    @ResponseBody
    @RequestMapping("/addTrafficreCords")
    public Result addTrafficreCords(Trafficrecords trafficrecords){
        LocalDateTime localDateTime = LocalDateTime.now();
        String endtime = localDateTime.toString();
        trafficrecords.setEndtime(endtime);
    try {
        System.out.println(trafficrecords);
        int i = trafficreCordsService.addTraff(trafficrecords);
        return Result.success("成功");
    }catch (Exception e){
        e.printStackTrace();
        return Result.success("失败");
    }
    }



}
